<?php
namespace CountLike\Controller;
use Common\Controller\CountLikeController;
class RedpacketController extends CountLikeController {


    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     *  获取基本信息
     */
    public function getinfo(){

        $appid = I('post.appid','','htmlspecialchars');
        //规则
        $data['rule'] = array('完成挑战即可获得随机红包','累计金额达到5元可以发起提现。','提现请添加工作人员微信ONETECHCLUB，工作人员将定时为您发放红包奖励。','放假期间回复可能有延误，我们工作人员看到消息会第一时间为您发放红包，请见谅！');

        $data['indexshare1'] = "邀请好友一起领红包"; //首页弹出框分享按钮1
        $data['indexshare2'] = "邀请好友一起来玩"; //首页弹出框分享按钮2

        $data['fenxcj'] = "分享成绩"; //挑战失败时弹出

        $data['sharelive'] = 0;//分享复活控制1显示0隐藏

        $data['sharelivetitle'] = '求助群友';//分享复活文案

        $data['ranktitle1'] = '今日排行'; //排行第一个标题

        $data['ranktitle2'] = '端午得奖公示'; //排行第2个标题

        $data['moreplay'] = $this->extension(); //更多好玩

        $data['advertising'] = $this->advertising(); //广告

        $data['tousu'] = array(
            array(
                'title'=>'色情',
                'id'=>'1',
            ),
            array(
                'title'=>'骚扰',
                'id'=>'2',
            ),
            array(
                'title'=>'欺诈',
                'id'=>'3',
            ),
            array(
                'title'=>'传播谣言',
                'id'=>'4',
            ),
            array(
                'title'=>'内容不符',
                'id'=>'5',
            ),
            array(
                'title'=>'恶意收集信息',
                'id'=>'6',
            ),
            array(
                'title'=>'侵权',
                'id'=>'7',
            ),
            array(
                'title'=>'诱导',
                'id'=>'8',
            ),

        );

        $data['sharetem'] = array(
            'title'=>'答题领红包',
            'desc'=>'锻炼你的大脑',
            'imgUrl'=>$this->url.'share/susuanshare.jpg',
        );



        $this->returnData($data);

    }

    /**
     * 截屏
     */
    public function getscreen(){

        $appid = I('post.appid','','htmlspecialchars');
        $uid = I('post.uid','','int');


        if (!$appid||!$uid){
            $this->returnData('','非法操作',400);
        }
        $data['screen'] = 1;
        $data['screen_time'] = time();

        M('countlike_member')->where(array('countlike_member_id'=>$uid))->save($data);

        $data['status'] = 1;
        $this->returnData($data);
    }

    //投诉
    public function tousu(){

        $data = array();
        $data['uid']= I('post.uid','','int');

        $data['appid'] = I('post.appid','','htmlspecialchars');

        $data['tid'] = I('post.id','','int');

        $data['title'] = I('post.title','','htmlspecialchars');

        $data['create_time'] = date('Y-m-d H:i:s',time());

        M('countlike_tousu')->add($data);

        $this->returnData();
    }

    //提现记录
    public function cash(){

        $field                          =   array('nickname','avatarurl','money');
        $where_cash_log['cash_status']  =   array('EGT','0');
        $result   =   M('countlike_cash')->where($where_cash_log)->field( $field )->select();

        foreach ($result as $key=>$val){

            $result[$key]['title'] = '成功提现'.$result[$key]['money'].'元';

        }

        $this->returnData($result);

    }

    //获取机会
    public function prize_num()
    {
        $member_id        =   I('post.uid','','htmlspecialchars');
        if (! empty($member_id))
        {
            $where          =   array();
            $data           =   array();
            $where['countlike_member_id']   =   $member_id;
            $field          =   array('countlike_member_id','openid','game_num','totle_num','prize_num','money_total','play_time','play_num','total_money');
            $member_data    =   M('countlike_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                $today_time     =   strtotime(date('Y-m-d',time()));
                //限制用户一天玩的次数
                if ($member_data['play_time'] > $today_time && $member_data['play_num'] >= 10)
                {
                    $data['game_num']   =   $member_data['game_num'];
                    $data['totle_num']  =   $member_data['totle_num'];
                    $data['money_total']=   $member_data['money_total'];
                    $data['prize_num']  =   $member_data['prize_num'];
                    $this->returnData($data,$msg='当天挑战次数已上限,明天再来',$code=400);
                }

                $data['game_num']   =   $member_data['game_num'];

                if(empty($member_data['game_num']) && $member_data['play_time'] < $today_time)
                {
                    $data['game_num']   =   1;
                }

                if (empty($member_data['game_num'])||$member_data['game_num']==0) {

                    if (empty($member_data['total_money']) || $member_data['total_money'] <1) {  //金额<1 随便玩
                        $data['game_num'] = 1;
                    }
                }


                $data['totle_num']  =   $member_data['totle_num'];
                $data['money_total']=   $member_data['money_total'];
                $data['prize_num']  =   $member_data['prize_num'];

                $this->returnData($data,$msg='OK',$code=200);
            }
        }

        $this->returnData($data,$msg='用户id参数不能为空',$code=404);
    }


    /**
     * 获取题目进行挑战
     */
    public function new_problem(){

        $member_id = I('post.uid','','int');
        $share_uid  =   I('post.share_uid','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');


        if (!$member_id||!$appid){
            $this->returnData('','参数错误',401);
        }

        $today_time     =   strtotime(date('Y-m-d',time()));

        $info = M('countlike_member')->field('game_num,nickname,avatarurl,play_time,play_num,appid,total_money,totle_num')->where(array('countlike_member_id'=>$member_id))->find();//获取挑战机会
        $data = array();
        if ($info){
            //绑定好友
            if (! empty( $share_uid) &&  $share_uid != $member_id)
            {
                $this->share_fraend($share_uid,$member_id,$appid);
            }

            if ($info['game_num'] > 0 || ($info['play_time'] < $today_time)|| $info['total_money']<1 ) {

                $update = array();
                if ($info['play_time'] >= $today_time&&$info['game_num']>0)
                {
                    $update['game_num'] = $info['game_num'] - 1;
                }

                if ($info['play_time'] >= $today_time) {  //判断今天是否玩过
                    $game_num = $info['game_num'];
                    $update['play_num'] = (int)$info['play_num'] + 1;
                } else {
                    $game_num = 1 + $info['game_num'];
                    $update['play_num'] = 1;
                }


                if (empty($info['game_num'])) {

                    if (empty($info['total_money']) || $info['total_money'] <1) {  //金额为0 随便玩
                        $game_num = 1;
                        $update['play_num'] = $info['play_num'];
                    }
                }
            }

            $time = time();

            $update['play_time'] = $time;

            $res = array();
            if ($game_num>0){
                $update['totle_num']   =   $info['totle_num']   +   1;

                M('countlike_member')->where(array('countlike_member_id'=>$member_id))->save($update);
                $res['question'] = $this->redpacketquestion($member_id,$time,$appid);
                $msg = "ok";
                $res['game_num'] = $game_num;
                $res['success_number'] = count($res['question']);
            }else{
                $res['game_num'] = $game_num;
                $msg = "挑战机会已用完";//分享到群获得更多机会
            }

            $this->returnData($res,$msg,200);
        }
        $this->returnData($data,$msg='用户id参数不能为空',$code=404);

    }

    /**
     * 分享好友
     * @param $share_uid
     * @param $uid
     */
    private function share_fraend($share_uid,$uid,$appid)
    {
        $where          =   array();
        $where['countlike_member_id']   =   $share_uid;
        $field          =   array('countlike_member_id');
        $member_data    =   M('countlike_member')->where($where)->field($field)->find();
        if (! empty($member_data))
        {
            $where_relationship                         =   array();
            $where_relationship['my_member_id']         =   $share_uid;
            $where_relationship['coupling_member_id']   =   $uid;
            $field_relationship                         =   array('countlike_relationship_id');
            $result     =   M('countlike_relationship')->where($where_relationship)->field($field_relationship)->find();

            if (empty($result))
            {
                $insert_data                        =   array();
                $insert_data['my_member_id']        =   $share_uid;
                $insert_data['coupling_member_id']  =   $uid;
                $insert_data['createtime']          =   time();
                $insert_data['appid']               =  $appid;
                M('countlike_relationship')->add($insert_data);
            }
        }
    }



    //回答
    public function new_answer()
    {

        $member_id = I('post.uid','','int');

        $answer = $_POST['answer'];

        if (!$member_id||!$answer){
            $this->returnData('','非法操作',401);
        }

        $filed = array('appid','play_time','money_total','total_money','prize_num');

        $memberdata = M('countlike_member')->field($filed)->where(array('countlike_member_id'=>$member_id))->find();

        if (!$memberdata){
            $this->returnData('','用户不存在',100);
        }
        $where['countlike_member_id'] = $member_id;
        $where['create_time'] = $memberdata['play_time'];
        $where['appid'] = $memberdata['appid'];
        $where['state'] = 0;

        $baoanswer = M('countlike_questions_played')->field('anwser,id')->where($where)->find();


        if (json_decode($answer)!=json_decode($baoanswer['anwser'])){ //验证题目是否全对
            $data['challenge_id']   =   0;
            $this->returnData($data,'答错了',100);
        }

        //更新通关数
        $update_data                =   array();
        $update_data['prize_num']   =   $memberdata['prize_num']   +   1;
        M('countlike_member')->where(array('countlike_member_id'=>$member_id))->save($update_data);

        M('countlike_questions_played')->where($where)->save(array('state'=>'1')); //更新题目状态

        //获取红包
        $data['challenge_id']   =   $this->create_redbag($member_id,$memberdata['appid'],$baoanswer['id']);
        $data['msg']            =   '恭喜你通过所有关卡';

        $this->returnData($data,$msg='恭喜你通过所有关卡',$code=200);

    }



    /**
     * 获取红包
     */
    public function award_total()
    {
        $uid                =   I('post.uid','','htmlspecialchars');
        $challenge_id       =   I('post.challenge_id','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');

        $data                   =   array();
        if (! empty($challenge_id) && ! empty($uid))
        {

            $where                  =   array();
            $field                  =   array('red_total','status','total_sn','create_time','red_total');
            $where['member_id']     =   $uid;
            $where['challenge_id']  =   $challenge_id;
            $newsong_redbag         =   M('countlike_redbag')->where($where)->field($field)->find();
            if (! empty($newsong_redbag))
            {
                //更新数据
                if (empty($newsong_redbag['status']))
                {
                    $where_member           =   array();
                    $field                  =   array('money_total','total_money');
                    $where_member['countlike_member_id']    =   $uid;
                    $member_data            =   M('countlike_member')->where($where_member)->field($field)->find();

                    if (! empty($member_data))
                    {
                        //开启事务更新数据
                        $Model = M();
                        $Model->startTrans();

                        //更新红包
                        $update_redbag                  =   array();
                        $update_redbag['status']        =   1;
                        $update_redbag['update_time']   =   date('Y-m-d H:i:s',time());

                        $result     =  $Model->table('mj_countlike_redbag')->where($where)->save($update_redbag);
                        if (! empty($result))
                        {
                            //更新红包记录
                            $update_log                  =   array();
                            $update_log['status']        =   1;
                            $update_log['update_time']   =   date('Y-m-d H:i:s',time());
                            $result     =   $Model->table('mj_countlike_redbag_log')->where($where)->save($update_log);

                            if (! empty($result))
                            {
                                //更新用户数据
                                $update_member                  =   array();
                                $update_member['money_total']   =   $member_data['money_total'] + $newsong_redbag['red_total'];
                                $update_member['total_money']   =   $member_data['total_money'] + $newsong_redbag['red_total'];
                                $update_member['updatetime']   =   time();
                                $result     =   $Model->table('mj_countlike_member')->where($where_member)->save($update_member);

                                if (! empty($result))
                                {
                                    //更新收支记录
                                    $where_change_log                   =   array();
                                    $where_change_log['from_sn']        =   $newsong_redbag['total_sn'];
                                    $update_change_log                  =   array();
                                    $update_change_log['status']        =   1;
                                    $update_change_log['update_time']   =   date('Y-m-d H:i:s',time());
                                    $Model->table('mj_countlike_change_log')->where($where_change_log)->save($update_change_log);
                                    $Model->commit();
                                }
                                else
                                {
                                    $Model->rollback();
                                }
                            }
                            else
                            {
                                $Model->rollback();
                            }
                        }
                        else
                        {
                            $Model->rollback();
                        }

                    }
                }

                $data               =   $newsong_redbag['red_total'];
                $this->returnData($data,$msg='ok',$code=200);
            }
        }
        $this->returnData($data,$msg='参数错误',$code=401);
    }

    /**
     * 红包记录
     */
    public function redbag_log()
    {
        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');
        if(! empty($uid))
        {
            $where          =   array();
            $where['countlike_member_id']   =   $uid;
            $field          =   array('countlike_member_id');
            $result         =   M('countlike_member')->where($where)->field($field)->find();

            if (! empty($result))
            {
                $where_redbag_log               =   array();
                $field2                          =   array('id','challenge_id','game_id','member_id','red_total','status','create_time','update_time','total_sn','time');
                $where_redbag_log['member_id']  =   $uid;
                $where_redbag_log['status']     =   1;
                $data   =   M('countlike_redbag_log')->where($where_redbag_log)->field($field2)->select();
            }
        }

        $this->returnData($data,$msg='ok',$code=200);
    }


    /**
     * 提现
     */
    public function get_cash()
    {
        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $money      =   I('post.money','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');
        if (! empty($uid) && ! empty($money) && $money > 0)
        {
            $where          =   array();
            $where['countlike_member_id']   =   $uid;
            $field          =   array('countlike_member_id','money_total','lock_money','nickname','avatarurl');
            $member_data    =   M('countlike_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                //判断是否有体现申请
                $where_cash                 =   array();
                $field_cash                 =   array('id','order_id');
                $where_cash['uid']          =   $uid;
                $where_cash['cash_status']  =   0;
                $newsong_cash               =   M('countlike_cash')->where($where_cash)->field($field_cash)->find();

                if (! empty($newsong_cash))
                {
                    $data['msg']        =   '你有一笔提现申请再审核中，不能继续提现';
                    $data['order_id']   =   $newsong_cash['order_id'];
                    $this->returnData($data,$msg='你有一笔提现申请再审核中，不能继续提现',$code=400);
                }
                else
                {
                    //判断金额限制
                    if ($member_data['money_total'] < $money)
                    {
                        $data['msg']    =   '非法操作金额';
                        $this->returnData($data,$msg='非法操作金额!',$code=400);
                    }

                    //5元以上才能提现
                    if ($member_data['money_total'] < 5)
                    {
                        $data['msg']    =   '金额要5元以上才能提现';
                        $this->returnData($data,$msg='Think You !',$code=400);
                    }

                    //插入数据
                    $insert_order                   =   array();
                    $insert_order['uid']            =   $uid;
                    $insert_order['nickname']       =   $member_data['nickname'];
                    $insert_order['avatarurl']      =   $member_data['avatarurl'];
                    $insert_order['order_id']       =   'TX'.date('YmdHis').rand(100000,999999);
                    $insert_order['money']          =   $money;
                    $insert_order['create_time']    =   time();
                    $insert_order['appid']    =   $appid;

                    //更新数据
                    $update_member                  =   array();
                    $update_member['lock_money']    =   $money;
                    $update_member['money_total']   =   $member_data['money_total'] -  $money;
                    $update_member['updatetime']   =   time();

                    //开启事务
                    $Model = M();
                    $Model->startTrans();

                    $result_cash        =   $Model->table('mj_countlike_cash')->add($insert_order);

                    $result_member      =   $Model->table('mj_countlike_member')->where($where)->save($update_member);

                    if (! empty($result_cash) && ! empty($result_member))
                    {
                        $Model->commit();

                        //发送推送消息（用户的和客服的）


                        $data['msg']    =   '提现成功！等待审核！';
                        $data['kefu']   =   'onetechclub';
                        $data['order_id']   =   $insert_order['order_id'];
                        $this->returnData($data,$msg='Think you !',$code=200);
                    }
                    else
                    {
                        $Model->rollback();
                        $data['msg']    =   '提现失败';
                        $this->returnData($data,$msg='提现失败',$code=400);
                    }

                }

            }
        }

        $data['msg']    =   '非法操作';
        $this->returnData($data,$msg='非法操作',$code=400);
    }

    //提现记录
    public function cash_log()
    {
        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');
        if(! empty($uid))
        {
            $where          =   array();
            $where['countlike_member_id']   =   $uid;
            $field          =   array('countlike_member_id');
            $result         =   M('countlike_member')->where($where)->field($field)->find();

            if (! empty($result))
            {
                $where_cash_log                 =   array();
                $field                          =   array('id','uid','money','order_id','create_time','cash_status');
                $where_cash_log['uid']          =   $uid;
                $where_cash_log['cash_status']  =   array('EGT','0');
                $result   =   M('countlike_cash')->where($where_cash_log)->field( $field )->select();
                if (! empty($result))
                {
                    foreach ($result as $key=> $val)
                    {
                        $val['create_time'] =   date('Y-m-d H:i:s',$val['create_time']);
                        $data[] =   $val;
                    }

                }
            }
        }

        $this->returnData($data,$msg='ok',$code=200);

    }

    //分享群
    public function get_numbers()
    {

        $data       =   array();
        $uid        =   I('post.uid','','htmlspecialchars');
        $endata     =   I('post.endata','','htmlspecialchars');
        $shareType = I('post.shareType','2','int');
        $vi         =   I('post.vi','','htmlspecialchars');


        if (! empty($endata) && ! empty($uid) && ! empty($vi))
        {
            $where          =   array();
            $where['countlike_member_id']   =   $uid;

            $field          =   array('countlike_member_id','openid','session_key','game_num','play_time','appid');
            $member_data    =   M('countlike_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {

                //解析openGId
                $openGId = $this->getopenGId($member_data['appid'], $member_data['session_key'], $endata, $vi);


                if ($openGId != '-1')
                {
                    if ($shareType=='1'){
                        $time = date('Y-m-d',time());

                        $where2['sharetime'] = $time;
                        $where2['countlike_member_id'] = $uid;
                        $where2['opengid'] = $openGId;

                        $interior = $record = M('countlike_activity_interior')->where($where2)->getField('id'); //判断是否已分享
                        if ($interior){
                            M('countlike_activity_interior')->where($where2)->setInc('sharenum');
                            $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
                        }
                        //如果没分享过则插入
                        $data = array(
                            'opengid'=>$openGId,
                            'sharetime'=>date('Y-m-d',time()),
                            'sharenum'=>1,
                            'countlike_member_id'=>$uid,
                            'appid'=>$member_data['appid'],
                        );

                        M('countlike_activity_interior')->data($data)->add();
                        $this->returnData(array('repetition'=>'repetition'),'继续挑战',200);
                    }

                    //创建群
                    $where_group                =   array();
                    $where_group['countlike_member_id']         =   $uid;
                    $where_group['opengid']     =   $openGId;
                    $where_group['sharetime']   =   date('Y-m-d');
                    $field                      =   array('id','opengid','sharenum','sharetime');
                    $group_data                 =   M('countlike_wechat_group')->where($where_group)->field($field)->find();
                    if (! empty($group_data))
                    {
                        $update_data                =   array();
                        $update_data['sharetime']   =   date('Y-m-d',time());
                        $update_data['sharenum']    =   $group_data['sharenum'] +   1;
                        M('countlike_wechat_group')->where($where_group)->save($update_data);

                        $msg='本群已分享过';

                        $this->returnData($data,$msg,$code=400);
                    }
                    else
                    {
                        $insert_data                =   array();
                        $insert_data['opengid']     =   $openGId;
                        $insert_data['countlike_member_id']         =   $uid;
                        $insert_data['sharetime']   =   date('Y-m-d',time());
                        $insert_data['sharenum']    =   1;
                        $insert_data['create_time'] =   time();
                        $insert_data['appid'] =   $member_data['appid'];
                        $result     =   M('countlike_wechat_group')->add($insert_data);

                        if (! empty($result))
                        {
                            //加机会
                            $update_member                  =   array();
                            $update_member['updatetime']   =   time();
                            $update_member['game_num']      =   $member_data['game_num']    +   1;
                            $result     =   M('countlike_member')->where($where)->save($update_member);

                            if (! empty($result))
                            {
                                $msg            =   '分享成功';
                                $data['msg']    =   '分享成功';

                                $this->returnData($data,$msg,$code=200);
                            }
                        }
                    }
                }
            }
        }

        $this->returnData($data,$msg='已分享',$code=400);
    }


    /**
     * 挑战()
     */
    public function change(){

        $member_id = I('post.uid','','int');

        $share_uid  =   I('post.share_uid','','htmlspecialchars');
        $appid = I('post.appid','','htmlspecialchars');


        if (!$member_id){
            $this->returnData('','参数错误',401);
        }


        $date = date('Y-m-d',time());

        //绑定好友
        if (! empty( $share_uid) &&  $share_uid != $member_id)
        {
            $this->share_fraend($share_uid,$member_id,$appid);
        }

        $where['countlike_member_id'] = $member_id;
        $where['signin_time'] = $date;

        $issign="";
        $issign = M('countlike_signin')->where($where)->getField('signin_num'); //判断今天有没有签到
        if (!$issign||$issign==""){
            $data = array(
                'countlike_member_id'=>$member_id,
                'signin_time'=>$date,
                'signin_num'=>'1'
            );

            M('countlike_signin')->data($data)->add();

            M('countlike_member')->where(array('countlike_member_id'=>$member_id))->save(array('giveopportunity'=>'1')); //每天赠送机会
        }

        $member_data = M('countlike_member')->field('game_num,giveopportunity,nickname,avatarurl,total_money')->where(array('countlike_member_id'=>$member_id))->find();//获取挑战机会

        $game_num = $member_data['game_num'];
        if (empty($member_data['game_num'])) {

            if (empty($member_data['total_money']) || $member_data['total_money'] <1) {  //金额为0 随便玩
                $game_num = 1;
            }
        }

        $chang = $game_num+$member_data['giveopportunity'];

        if ($chang>0){
            if ($member_data['giveopportunity']>0){
                M('countlike_member')->where(array('countlike_member_id'=>$member_id))->setDec('giveopportunity'); //先减去赠送的
            }else{
                if ($member_data['game_num']>0){
                    M('countlike_member')->where(array('countlike_member_id'=>$member_id))->setDec('game_num'); //挑战机会减一
                }

            }

            $insert = array(
                'countlike_member_id'=>$member_id,
                'num'=>0,
                'date'=>$date,
                'week'=>(int)date('W'),
                'year'=>(int)date('Y'),
                'nickname'=>$member_data['nickname'],
                'avatarurl'=>$member_data['avatarurl'],
                'createtime'=>date('Y-m-d H:i:s',time())
            );
            $id = M('countlike_change')->data($insert)->add();

            $wherech['countlike_member_id'] = $member_id;
            $wherech['date'] = $date;
            $record = M('countlike_change')->where($wherech)->max('num');

            $isshow = 0; //分享复活
            $sharetitle = '求助群友';

            $res['set'] = array(
                'opportunity'=>$chang,
                'time'=>120,//挑战时间
                'record'=>$record,
                'chang_id'=>$id,
                'isshow'=>$isshow,
                'sharetitle'=>$sharetitle
            );
            $res['question'] = $this->activeityquestion();
            $this->returnData($res,'成功',200);
        }

        $this->returnData(array('opportunity'=>$chang),'挑战机会已用完',101);

    }

    /**
     * 微信群分享记录(晋级赛)
     */
    public function sharerecord(){

        $member_id = I('post.member_id','','int');
        $shareType = I('post.shareType','2','int');
        $encryptedData = $_POST['encryptedData'];
        $iv = $_POST['iv'];


        if (!$encryptedData||!$member_id||!$iv){
            $this->returnData('','参数错误',401);
        }

        $info = M('countlike_member')->field('appid,session_key')->where(array('countlike_member_id'=>$member_id))->find();

        $appid = $info['appid'];
        $sessionkey = $info['session_key'];

        $openGId = $this->getopenGId($appid,$sessionkey,$encryptedData,$iv);

        if ($openGId=='-1'||empty($openGId)){
            $this->returnData('','',100);
        }

        $time = date('Y-m-d',time());

        $where['sharetime'] = $time;
        $where['countlike_member_id'] = $member_id;
        $where['opengid'] = $openGId;

        if ($shareType=='1'){
            $interior = $record = M('countlike_activity_interior')->where($where)->getField('id'); //判断是否已分享
            if ($interior){
                M('countlike_activity_interior')->where($where)->setInc('sharenum');
                $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
            }
            //如果没分享过则插入
            $data = array(
                'opengid'=>$openGId,
                'sharetime'=>$time,
                'sharenum'=>1,
                'countlike_member_id'=>$member_id,
            );

            M('countlike_activity_interior')->data($data)->add();
            $this->returnData(array('repetition'=>'repetition'),'立即复活',200);
        }


        $where2['sharetime'] = $time;
        $where2['countlike_member_id'] = $member_id;

        $count = M('countlike_wechat_group')->where($where2)->count();

        $maxshare = 10;

        if ($count>=$maxshare){
            $this->returnData(array('repetition'=>'out'),'一天有效分享次数为10',201);
        }


        $record = "";
        $record = M('countlike_wechat_group')->where($where)->getField('sharenum'); //判断是否已分享

        //如果已分享则自增分享次数然后返回
        if ($record){
            M('countlike_wechat_group')->where($where)->setInc('sharenum');

            $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
        }


        //如果没分享过则插入，用户挑战机会加一
        $data = array(
            'opengid'=>$openGId,
            'sharetime'=>$time,
            'sharenum'=>1,
            'countlike_member_id'=>$member_id,
            'appid'=>$appid,
        );

        M('countlike_wechat_group')->data($data)->add();

        M('countlike_member')->where(array('countlike_member_id'=>$member_id))->setInc('game_num'); //挑战机会加一

        $opportunity = M('countlike_member')->field('game_num')->where(array('countlike_member_id'=>$member_id))->find();//获取挑战机会


        $this->returnData(array('repetition'=>'no','game_num'=>$opportunity['game_num']),'分享成功',200);
    }



//    /**
//     * 好友关联
//     */
//    public function friendsassociated(){
//
//        $uid = I('post.my_member_id','','int');
//        $coupling_uid = I('post.coupling_member_id','','int');
//        $appid = I('post.appid','','htmlspecialchars');
//
//        if ($uid==""||$coupling_uid==""){
//            $this->returnData('','参数不能为空',401);
//        }
//
//        if ($uid==$coupling_uid){
//            $this->returnData('','自己不用关联自己',100);
//        }
//
//
//        $where['my_member_id']=$uid;
//        $where['coupling_member_id']=$coupling_uid;
//        $where['appid'] = $appid;
//
//        $where1['my_member_id']=$coupling_uid;
//        $where1['coupling_member_id']=$uid;
//        $where1['appid'] = $appid;
//
//        $relevancy = M('countlike_relationship')->field('countlike_relationship_id')->where($where)->find();
//
//        $relevancy1 = M('countlike_relationship')->field('countlike_relationship_id')->where($where1)->find();
//        //判断是否已关联(有待优化)
//        if ($relevancy||$relevancy1){
//            $this->returnData('','已关联',200);
//        }
//
//        $data = array(
//            'my_member_id'=>$uid,
//            'coupling_member_id' =>$coupling_uid,
//            'appid'=>$appid,
//            'createtime' =>time(),
//        );
//
//        $insert = M('countlike_relationship')->data($data)->add();
//
//        if ($insert){
//            $this->returnData('','关联成功',200);
//        }else{
//            $this->returnData('','关联失败',100);
//        }
//
//    }




    //更多好玩
    private function extension()
    {
        $data = M('newsong_app')->select();

        foreach ($data as $key=>$val){

            $data[$key]['logo'] = $this->url.$data[$key]['logo'];

        }
        return $data;
    }

    /**
     * 广告
     */
    private function advertising(){

        $data = M('newsong_ad')->where(array('isshow'=>'1'))->select();

        foreach ($data as $key=>$val){

            $data[$key]['logo'] = $this->url.$data[$key]['logo'];

        }

        return $data;

    }




}